package com.lvmama.pangolin.controller;

import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.HBaseQueryPrice.HBaseHotelHistoryPrice;
import com.lvmama.pangolin.HBaseQueryPrice.HBaseTicketHistoryPrice;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.entity.PredictCommodityListConditionVO;
import com.lvmama.pangolin.entity.PriceVO;
import com.lvmama.pangolin.entity.predict.CommodityPricePredict;
import com.lvmama.pangolin.predict.service.CommodityPricePredictService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @Description: TODO
 * @Author: sunpengpeng
 * @Date: Create in 2017/10/16 15:20
 * @Modified By:
 */

@Controller
@RequestMapping("datashow")
public class PredictCommodityController {

    private static Logger log = Logger.getLogger(PredictCommodityController.class);

    @Autowired
    private CommodityPricePredictService commodityPricePredictService;

    @Autowired
    private HBaseHotelHistoryPrice historyHotelPrice;

    @Autowired
    private HBaseTicketHistoryPrice historyTicketPrice;

    @RequestMapping("predictCommodityList")
    public String predictCommodityList(ModelMap model, PredictCommodityListConditionVO predictCommodityListConditionVO) throws Exception {
        if (StringUtils.isEmpty(predictCommodityListConditionVO.getBusinessCode())) {
            predictCommodityListConditionVO.setBusinessCode(Constant.TICKE_BUS_TYPE);
        }
        if (StringUtils.isEmpty(predictCommodityListConditionVO.getCompanyCode())) {
            predictCommodityListConditionVO.setCompanyCode(Constant.LMM_C_CODE);
        }

        PageInfo<CommodityPricePredict> pageInfo = commodityPricePredictService.queryCommodityPredictList(predictCommodityListConditionVO);
        model.addAttribute("page", pageInfo);
        model.addAttribute("resultList", pageInfo.getList());
        model.addAttribute("PredictCommodityListConditionVO", predictCommodityListConditionVO);
        return "predict/predictCommodity";
    }

    /**
     * 异步请求历史商品价格
     *
     * @param productId
     * @return
     */
    @RequestMapping("/predictCommodityHisList")
    @ResponseBody
    public Object commodityPriceHistory(String productId, String commodityId, String chooseTime, String businessCode, String companyCode) {
        Map<String, Object> result = new HashMap<String, Object>();
        List<PriceVO> priceRealList = new ArrayList<PriceVO>();
        List<PriceVO> pricePredictList = new ArrayList<PriceVO>();
        //查找历史真实数据
        if ("TICKET".equals(businessCode)) {
            companyCode = companyCode.substring(0, companyCode.indexOf("_")).toLowerCase();
            String tableName = String.format(Constant.TICKET_COMMODITY_H_TEMPLATE, companyCode);
            String family = Constant.HB_VARIABLE_FAMILY;
            List<Map<String, String>> ticketHisDataList = historyTicketPrice.queryCommodityByDate(chooseTime, commodityId, tableName, family);
            if (!CollectionUtils.isEmpty(ticketHisDataList)) {
                Collections.sort(ticketHisDataList, new Comparator<Map<String, String>>() {
                    @Override
                    public int compare(Map<String, String> o1, Map<String, String> o2) {
                        if (o1.get("insert_time") != null) {
                            return o1.get("insert_time").compareTo(o2.get("insert_time"));
                        } else if (o1.get("createTime") != null) {
                            return o1.get("createTime").compareTo(o2.get("createTime"));
                        } else if (o1.get("create_time") != null) {
                            return o1.get("create_time").compareTo(o2.get("create_time"));
                        }
                        return 0;
                    }
                });
                priceRealList = handlePriceData(ticketHisDataList, priceRealList);
            }
            result.put("priceRealList", priceRealList);
        } else if ("HOTEL".equals(businessCode)) {
            String tableName = Constant.HOTEL_COMMODITY_H;
            List<Map<String, String>> hotelHisDataList = historyHotelPrice.queryHotelCommodityByRange(commodityId, productId, companyCode, tableName, chooseTime);
            if (!CollectionUtils.isEmpty(hotelHisDataList)) {
                Collections.sort(hotelHisDataList, new Comparator<Map<String, String>>() {
                    @Override
                    public int compare(Map<String, String> o1, Map<String, String> o2) {
                        return o1.get("createTime").compareTo(o2.get("createTime"));
                    }
                });
                priceRealList = handlePriceData(hotelHisDataList, priceRealList);
            }
            result.put("priceRealList", priceRealList);
        }
        //查找预测数据
        List<CommodityPricePredict> pricePredictDataList = commodityPricePredictService.queryCommodityListById(commodityId);
        for (CommodityPricePredict commodityPricePredict : pricePredictDataList) {
            PriceVO price = new PriceVO();
            price.setSale_date(DateUtil.formatDate(commodityPricePredict.getPreDate(), "yyyyMMdd"));
            price.setPriceType("predictPrice");
            price.setSale_price(Double.valueOf(commodityPricePredict.getPrePrice()));
            pricePredictList.add(price);
        }
        result.put("pricePredictList", pricePredictList);
        return result;
    }

    private List<PriceVO> handlePriceData(List<Map<String, String>> map, List<PriceVO> priceList) {
        String grab_time = "";
        for (Map<String, String> data : map) {
            if (data.get("insert_time") != null) {
                grab_time = data.get("insert_time");
            } else if (data.get("create_time") != null) {
                grab_time = data.get("create_time");
            } else if (data.get("updateTime") != null) {
                grab_time = data.get("updateTime");
            }

            if (data.get("sale_price") != null && !"".equals(data.get("sale_price"))) {
                PriceVO price = new PriceVO();
                price.setSale_date(grab_time);
                price.setPriceType("realPrice");
                price.setSale_price(Double.valueOf(data.get("sale_price")));
                priceList.add(price);
            } else if (data.get("salePrice") != null && !"".equals(data.get("salePrice"))) {
                PriceVO price = new PriceVO();
                price.setSale_date(grab_time);
                price.setPriceType("realPrice");
                price.setSale_price(Double.valueOf(data.get("salePrice")));
                priceList.add(price);
            }
        }
        return priceList;
    }
}
